package com.tjbklx1.thread.chap06.demo01;

import java.util.Random;

public class WriteThread extends Thread {

	private final Data data;
	private final String filler;
	private int index;

	public WriteThread(Data data, String filler) {
		super();
		this.data = data;
		this.filler = filler;
	}

	@Override
	public void run() {

		try {
			while (true) {
				char c = nextchar();
				data.write(c);
				Thread.sleep(new Random().nextInt(3000));
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	private char nextchar() {
		char c = filler.charAt(index);
		index++;
		if (index > filler.length()) {
			index = 0;
		}
		return c;
	}

}
